import {getToken} from '../util/StorageUtil';
import {
    action_login_success,
    action_re_login,
    update_user_info,
    update_menu_item,
    show_component,
    collapsed_layout
} from '../util/Constant';
import {deepCopy} from '../util/ObjUtil';

const defaultState = {
    isLogin: getToken(),
    username: null,
    userInfo: null,
    menuItem: null,
    collapsed: false
};

export default (state = defaultState, action) => {

    //登陆成功
    if (action.type === action_login_success) {
        let newState = deepCopy(state);
        newState.isLogin = action.isLogin;
        newState.username = action.username;
        return newState;
    }

    //需要重新登陆
    if (action.type === action_re_login) {
        let newState = deepCopy(state);
        newState.isLogin = action.isLogin;
        return newState;
    }

    //更新用户信息
    if (action.type === update_user_info) {
        let newState = deepCopy(state);
        newState.userInfo = action.userInfo;
        return newState;
    }

    //更新首页菜单
    if (action.type === update_menu_item) {
        let newState = deepCopy(state);
        newState.menuItem = action.menuItem;
        return newState;
    }

    //显示主要内容区的控件
    if (action.type === show_component) {
        let newState = deepCopy(state);
        newState.showComponent = action.showComponent;
        newState.showComponentPath = action.showComponentPath;
        return newState;
    }

    //菜单栏折叠
    if (action.type === collapsed_layout) {
        let newState = deepCopy(state);
        newState.collapsed = !state.collapsed;
        newState.showComponent = state.showComponent;
        return newState;
    }

    return state;
}